/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Copyright 2002-2003 by Mark Wolgemuth, All rights reserved.             */
/* Copyright 2002-2003 by Employease, Inc.  All rights reserved.           */
/*                                                                         */
/* Redistribution and use in source and binary forms, with or without      */
/* modification, are permitted provided that the following conditions      */
/* are met:                                                                */
/*                                                                         */
/*    1. Redistributions of source code must retain the above copyright    */
/* notice, this list of conditions and the following disclaimer.           */
/*    2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in the     */
/* documentation and/or other materials provided with the distribution.    */
/*                                                                         */
/* THIS SOFTWARE IS PROVIDED BY THE MOD_ATHENA PROJECT ``AS IS'' AND ANY   */
/* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE       */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR      */
/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR        */
/* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   */
/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,     */
/* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR      */
/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT            */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   */
/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef ATH_LB_H
#define ATH_LB_H

#include "apr.h"
#include "apr_hooks.h"
#include "apr_strings.h"
#include "apr_pools.h"
#include "apr_uri.h"
#include "apr_global_mutex.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
/* following removed for httpd-2.2 apr-1.2.0 */
/* #include "apr_compat.h" */

#include "ath_version.h"
#include "ath_util.h"
#include "ath_types.h"

#define      ATH_PKG_V           PACKAGE "-" VERSION "-#" ATH_SERIAL_STR
#define      ATH_PKG_V_LONG      ATH_PKG_V ": " ATH_STAMP 

/* this affects default online AND "up" setting for phys
 * and online status for farm
 */
#define      ATH_DEFAULT_STATUS  ATH_ON

/* these are here so they can be used by module and daemon */
#if APR_HAS_PROC_PTHREAD_SERIALIZE
#define        ATH_STR_LOCKMECH "proc_pthread"
static apr_lockmech_e ath_lockmech = APR_LOCK_PROC_PTHREAD;
#elif APR_HAS_POSIXSEM_SERIALIZE
#define        ATH_STR_LOCKMECH "posixsem"
static apr_lockmech_e ath_lockmech = APR_LOCK_POSIXSEM;
#elif APR_HAS_SYSVSEM_SERIALIZE
#define        ATH_STR_LOCKMECH "sysvsem"
static apr_lockmech_e ath_lockmech = APR_LOCK_SYSVSEM;
#else
#define        ATH_STR_LOCKMECH "default"
static apr_lockmech_e ath_lockmech = APR_LOCK_FLOCK;
//#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
//                  chown(path, unixd_config.user_id, -1)
#endif

/* farm.c */
ath_farm *     ath_farm_alloc(apr_pool_t *pool);
ath_cfg_farm * ath_farm_cfg_alloc(apr_pool_t *p);
ath_upd_farm * ath_farm_upd_alloc(apr_pool_t *p);
void           ath_farm_cache_init(apr_pool_t *pool);
void           ath_farm_cache_expire();
ath_cfg_farm * ath_farm_cfg_add(apr_pool_t *pool, 
				ath_list *cfgs, const char *arg);
ath_cfg_farm * ath_farm_cfg_set_expect(ath_cfg_farm *farm, int flag);
ath_cfg_farm * ath_farm_cfg_set_algo(ath_cfg_farm *farm, const char *algo);
ath_cfg_phys * ath_farm_cfg_add_member(apr_pool_t *pool, ath_cfg_farm *farm, 
				       ath_list *phys_cfgs, const char *arg);
void           ath_farm_format(ath_farm *farm);
ath_farm *     ath_farm_copy(ath_farm *dst, const ath_farm *src);
ath_farm *     ath_farm_configure(ath_farm *farm, ath_cfg_farm *f_cfg, 
				  int l_idx);
ath_farm *     ath_farm_for_name(char *name);
ath_farm *     ath_farm_for_idx(int idx);
ath_farm *     ath_farm_raw_for_idx(int idx);
char     *     ath_farm_to_str(apr_pool_t *p, ath_farm *farm, const char *fmt);
ath_farm *     ath_farm_clone_from_str(apr_pool_t *pool, char *str_farm);
void           ath_farm_add_member(ath_farm *farm, ath_phys *phys);
void           ath_farm_remove_member(ath_farm *farm, ath_phys *phys);
ath_phys *     ath_farm_member_for_idx(ath_farm *farm, int idx);

/* physical.c */
ath_phys *     ath_phys_alloc(apr_pool_t *p);
ath_cfg_phys * ath_phys_cfg_alloc(apr_pool_t *p);
ath_upd_phys * ath_phys_upd_alloc(apr_pool_t *p);
void           ath_phys_cache_init(apr_pool_t *pool);
void           ath_phys_cache_expire();
ath_cfg_phys * ath_phys_cfg_for_host(ath_list *phys_list, const char *host);
void           ath_phys_format(ath_phys *phys);
ath_phys *     ath_phys_copy(ath_phys *phys_dst, const ath_phys *phys_src);
ath_upd_phys * ath_phys_upd_clone_stats(ath_upd_phys *dst, ath_upd_phys *src);
ath_phys *     ath_phys_configure(ath_phys *phys, ath_cfg_phys *phys_cfg, 
				  int l_idx);
ath_phys *     ath_phys_for_host(char *host);
ath_phys *     ath_phys_for_idx(int idx);
ath_phys *     ath_phys_raw_for_idx(int idx);
int            ath_phys_idx_for_host(char *host);
int            ath_phys_upd_field_updated(const ath_upd_phys *physupd, 
					  const char field);
char *         ath_phys_to_str(apr_pool_t *p, ath_phys *phys, const char *fmt);
ath_phys *     ath_phys_clone_from_str(apr_pool_t *pool, char *str_phys);

/* algorithm.c */
ath_algo_def * ath_algo_def_alloc(apr_pool_t *);
void           ath_algo_def_format(ath_algo_def *);
ath_algo_def * ath_algo_def_from_str(ath_algo_def *algo, char *str_algo);
char *         ath_algo_def_to_str(apr_pool_t *p, ath_algo_def *algo);
ath_algo_def * ath_algo_def_copy(ath_algo_def *dst, const ath_algo_def *src);
int            ath_algo_rr(ath_farm *farm, int *ctx);
int            ath_algo_simple(ath_farm *farm, ath_algo_def *algo);
int            ath_algo_dynamic(apr_pool_t *p, 
				ath_farm *farm, ath_algo_def *algo);
int            ath_algo_get_chosen(apr_pool_t *p, 
				   ath_farm *farm, ath_algo_def *algo, 
				   int *rrctx);
char           ath_algo_str_to_type(const char *str_algo_piece);
char *         ath_algo_type_to_str(char c);
float          ath_algo_val_for_type(char c, const ath_phys *phys);

/* engine.c */
ath_engine *   ath_engine_get();
ath_engine_stat * ath_engine_stat_get();
void           ath_engine_format(ath_engine *engine);
ath_engine *   ath_engine_configure(apr_pool_t *p, ath_conf_ap *aconf);
char     *     ath_engine_to_str(apr_pool_t *p, const char *fmt);

/* shmemory.c (and needed precalc values) */
#define        ATH_SHM_SIZE         (sizeof(ath_shm_blob))
int            ath_shm_available();
int            ath_shm_create(apr_pool_t *p, const char *path);
int            ath_shm_attach(apr_pool_t *p, const char *path);
int            ath_shm_destroy();
ath_shm_blob * ath_shm_blob_get();
void           ath_shm_blob_format();
ath_farm *     ath_shm_farmbase();
ath_phys *     ath_shm_physbase();
ath_engine *   ath_shm_enginebase();
char     *     ath_shm_strncpy(char *dst, const char *src, int limit);

#endif // ATH_LB_H

